﻿using Sys.Workflow.Bpmn.Models;
using Sys.Workflow.Engine.Impl.Cfg;
using Sys.Workflow.Engine.Impl.Contexts;
using Sys.Workflow.Engine.Impl.DB;
using Sys.Workflow.Engine.Impl.Histories;
using Sys.Workflow.Engine.Impl.Interceptor;
using Sys.Workflow.Engine.Impl.Persistence.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sys.Workflow.Engine.impl.util
{
    /* Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */


    public class CommandContextUtil
    {
        public const string ATTRIBUTE_INVOLVED_EXECUTIONS = "ctx.attribute.involvedExecutions";
        public static ProcessEngineConfigurationImpl GetProcessEngineConfiguration()
        {
            return GetProcessEngineConfiguration(GetCommandContext());
        }
        public static ProcessEngineConfigurationImpl GetProcessEngineConfiguration(ICommandContext commandContext)
        {
            if (commandContext != null)
            {
                return (ProcessEngineConfigurationImpl)commandContext.ProcessEngineConfiguration;
            }
            return null;
        }

        public static IProcessDefinitionEntityManager GetProcessDefinitionEntityManager()
        {
            return GetProcessDefinitionEntityManager(GetCommandContext());
        }

        public static IProcessDefinitionEntityManager GetProcessDefinitionEntityManager(ICommandContext commandContext)
        {
            return GetProcessEngineConfiguration(commandContext).ProcessDefinitionEntityManager;
        }

        public static IHistoryManager GetActivityInstanceEntityManager()
        {
            return GetActivityInstanceEntityManager(GetCommandContext());
        }

        public static IHistoryManager GetActivityInstanceEntityManager(ICommandContext commandContext)
        {
            return GetProcessEngineConfiguration(commandContext).HistoryManager;
        }

        public static IExecutionEntityManager GetExecutionEntityManager()
        {
            return GetExecutionEntityManager(GetCommandContext());
        }

        public static IExecutionEntityManager GetExecutionEntityManager(ICommandContext commandContext)
        {
            return GetProcessEngineConfiguration(commandContext).ExecutionEntityManager;
        }

        public static ICommandContext GetCommandContext()
        {
            return Context.CommandContext;
        }

        public static void AddInvolvedExecution(ICommandContext commandContext, IExecutionEntity executionEntity)
        {

            if (executionEntity.Id != null)
            {
                Dictionary<string, IExecutionEntity> involvedExecutions = null;
                object obj = commandContext.GetAttribute(ATTRIBUTE_INVOLVED_EXECUTIONS);
                if (obj != null)
                {
                    involvedExecutions = (Dictionary<string, IExecutionEntity>)obj;
                }
                else
                {
                    involvedExecutions = new Dictionary<string, IExecutionEntity>();
                    commandContext.AddAttribute(ATTRIBUTE_INVOLVED_EXECUTIONS, involvedExecutions);
                }
                involvedExecutions.Add(executionEntity.Id, executionEntity);

            }
        }
    }
}
